import { reactive } from "vue";
import type { FormRules } from "element-plus";
import { checkConfigKey } from "@/api/system";

/** 自定义表单规则校验 */
export const formRules = reactive(<FormRules>{
  key: [
    { required: true, message: "配置名称为必填项", trigger: "blur" },
    { max: 30, message: "配置名称不能超过30字", trigger: "blur" },
    {
      validator: async (_, value, callback) => {
        if (value === "") {
          callback(new Error("配置名称为必填项"));
        } else {
          await checkConfigKey(value)
            .then(res => {
              if (!res) {
                callback(new Error("配置名称已存在"));
              } else {
                callback();
              }
            })
            .catch(() => {
              callback(new Error("配置名称已存在"));
            });
        }
      }
    }
  ],
  value: [
    { required: true, message: "配置内容为必填项", trigger: "blur" },
    { max: 100, message: "配置内容不能超过200字", trigger: "blur" }
  ],
  desc: [
    { required: true, message: "配置说明为必填项", trigger: "blur" },
    { max: 200, message: "配置说明不能超过200字", trigger: "blur" }
  ],
  type: [{ required: true, message: "配置类型为必填项", trigger: "blur" }]
});
